import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    routePath: [],
    menuList: [],
  },
  mutations: {
    // 保存数据
    updateData(state, data) {
      state.routePath = data
    },
    // 清除数据
    removeData(state) {
      state.routePath = []
    },
    addrouters(state) {
      let children = []
      let addroute = JSON.parse(localStorage.getItem('router'))
      if (addroute && addroute.length) {
        function forRouter(routers, child) {
          routers.forEach(item => {
            let subarr = []
            let Arrs = {
              path: item.path,
              name: item.path.slice(1),
              icon: item.icon,
              routname: item.name,
            }
            child.push(Arrs)
            if (item.children) {
              Arrs.children = subarr
              forRouter(item.children, subarr)
            } else {
              Arrs.component = resolve => require([`@/pages${item.route}.vue`], resolve)
            }
          })
        }
        forRouter(addroute, children)
      }
      state.menuList = children
    },
  },
  getters: {},
  actions: {},
  modules: {},
})
